<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/10
 * Time: 15:12
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Coupon extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $coupon_status = input('coupon_status');
            $keyword = input('keyword');
            
            $where['coupon_status'] = ['NEQ', 3];
            if(isset($coupon_status) && $coupon_status != ''){
                $where['coupon_status'] = $coupon_status;
            }
            if(isset($keyword) && $keyword != ''){
                $where['coupon_id|coupon_name'] = ['like', '%' . $keyword . '%'];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('coupon')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('coupon')
                ->field('coupon_id, coupon_name, coupon_use_price, coupon_dikou_price, coupon_type, coupon_total_num, 
                coupon_lingqu_num, coupon_status, coupon_is_send, coupon_send_time, coupon_range, coupon_use_end_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('coupon_time desc')
                ->select();
            foreach($lists as $k => $v){
                $use_num = 0; // 已使用数量
                $lose_num = 0; // 已失效数量
                $my_coupon = Db::table('up_my_coupon')
                    ->where('mc_coupon_id', $v['coupon_id'])
                    ->field('mc_status')
                    ->select();
                if($my_coupon){
                    foreach($my_coupon as $ke => $vo){
                        if($vo['mc_status'] == 2){
                            $use_num = $use_num + 1;
                        }
                        if($vo['mc_status'] == 3){
                            $lose_num = $lose_num + 1;
                        }
                    }
                }
                $lists[$k]['use_num'] = $use_num;
                $lists[$k]['lose_num'] = $lose_num;
                // 发送时间
                $lists[$k]['coupon_send_time'] = date('Y-m-d H:i', strtotime($v['coupon_send_time']));
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加优惠券
     * @return mixed|\think\response\Json
     */
    public function couponAdd(){
        if(Request::instance()->isPost()){
            $coupon_name = input('coupon_name'); // 优惠券名称
            $coupon_intro = input('coupon_intro'); // 简介
            $coupon_total_num = input('coupon_total_num'); // 总张数   0表示不限制
            $coupon_type = input('coupon_type'); // 1 满减   2 无门槛
            $coupon_use_price = input('coupon_use_price'); // 满减的可使用金额
            $coupon_dikou_price = input('coupon_dikou_price'); // 可抵扣金额
            $coupon_use_start_time = input('coupon_use_start_time'); // 使用开始日期
            $coupon_use_end_time = input('coupon_use_end_time'); // 使用结束日期
            $coupon_range = input('coupon_range'); // 1 部分用户   2 全部用户
            $coupon_user_ids = input('coupon_user_ids'); // 指定发送的用户ID
            $coupon_send_time = input('coupon_send_time'); // 发送时间
            if($coupon_dikou_price < 0.01 || $coupon_dikou_price > 10000){
                return json(['code' => 100, 'msg' => '请设置正确面值金额']);
            }
            if($coupon_type == 1){
                if($coupon_use_price <= $coupon_dikou_price){
                    return json(['code' => 100, 'msg' => '面值金额不能高于可使用金额']);
                }
            }
            if($coupon_use_start_time >= $coupon_use_end_time){
                return json(['code' => 100, 'msg' => '使用结束时间不可早于使用开始时间']);
            }
            $coupon = [
                'coupon_name' => $coupon_name,
                'coupon_intro' => $coupon_intro,
                'coupon_use_price' => $coupon_use_price,
                'coupon_dikou_price' => $coupon_dikou_price,
                'coupon_type' => $coupon_type,
                'coupon_total_num' => $coupon_total_num,
                'coupon_use_start_time' => $coupon_use_start_time,
                'coupon_use_end_time' => $coupon_use_end_time,
                'coupon_range' => $coupon_range,
                'coupon_send_time' => $coupon_send_time,
                'coupon_time' => date('Y-m-d H:i:s')
            ];
            if($coupon_range == 1){
                $coupon['coupon_user_ids'] = $coupon_user_ids;
            }else{
                $coupon['coupon_user_ids'] = -1;
            }
            $res = Db::table('up_coupon')->insert($coupon);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $user = Db::table('up_u_user')->where('user_status', 1)->field('user_id, user_username')->select();
        $this->assign('user', $user);
        return $this->fetch('coupon/couponadd');
    }
    
    /**
     * 领取详情
     * @return mixed|\think\response\Json
     */
    public function getList(){
        if(request()->isAjax ()){
            $where['mc_coupon_id'] = session('coupon_id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('my_coupon')
                ->join('up_coupon', 'coupon_id = mc_coupon_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('my_coupon')
                ->join('up_coupon', 'coupon_id = mc_coupon_id', 'LEFT')
                ->field('mc_id, mc_user_id, mc_status, mc_ling_time, coupon_send_time, mc_use_time, 
                mc_use_end_time, mc_ling_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('mc_ling_time desc')
                ->select();
            foreach($lists as $k => $v){
                // 发送时间
                $lists[$k]['coupon_send_time'] = date('Y-m-d H:i', strtotime($v['coupon_send_time']));
                // 领取时间
                $lists[$k]['mc_ling_time'] = date('Y-m-d H:i', strtotime($v['mc_ling_time']));
                // 订单编号、使用时间
                if($v['mc_status'] == 2){
                    $lists[$k]['order_number'] = Db::table('up_goods_order')
                        ->where('order_my_coupon_id', $v['mc_id'])
                        ->value('order_number');
                    $lists[$k]['mc_use_time'] = date('Y-m-d H:i', strtotime($v['mc_use_time']));
                }else{
                    $lists[$k]['order_number'] = '';
                    $lists[$k]['mc_use_time'] = '--';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $coupon_id = input('coupon_id');
        if(isset($coupon_id) && $coupon_id != ''){
            session('coupon_id', $coupon_id);
        }
        return $this->fetch('coupon/getlist');
    }
    
    /**
     * 编辑优惠券
     * @return mixed|\think\response\Json
     */
    public function couponEdit(){
        $coupon_id = input('coupon_id');
        $coupon = Db::table('up_coupon')
            ->where('coupon_id', $coupon_id)
            ->field('coupon_dikou_price, coupon_use_end_time, coupon_type, coupon_use_price, coupon_use_start_time')
            ->find();
        if(Request::instance()->isPost()){
            $coupon_dikou_price = input('coupon_dikou_price');
            $coupon_use_end_time = input('coupon_use_end_time');
            if($coupon_dikou_price < 0.01 || $coupon_dikou_price > 10000){
                return json(['code' => 100, 'msg' => '请设置正确面值金额']);
            }
            if($coupon['coupon_type'] == 1){
                if($coupon['coupon_use_price'] <= $coupon_dikou_price){
                    return json(['code' => 100, 'msg' => '面值金额不能高于可使用金额']);
                }
            }
            if($coupon['coupon_use_start_time'] >= $coupon_use_end_time){
                return json(['code' => 100, 'msg' => '使用结束时间不可早于使用开始时间']);
            }
            $update = [
                'coupon_dikou_price' => $coupon_dikou_price,
                'coupon_use_end_time' => $coupon_use_end_time,
                'coupon_updatetime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_coupon')->where('coupon_id', $coupon_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $this->assign([
            'coupon_id' => $coupon_id,
            'coupon' => $coupon
        ]);
        return $this->fetch('coupon/couponedit');
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function couponUp(){
        $coupon_id = input('id');
        $res = Db::table('up_coupon')->where('coupon_id', $coupon_id)->setField('coupon_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function couponDown(){
        $coupon_id = input('id');
        $res = Db::table('up_coupon')->where('coupon_id', $coupon_id)->setField('coupon_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function couponDel(){
        $coupon_id = input('id');
        $res = Db::table('up_coupon')->where('coupon_id', $coupon_id)->setField('coupon_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}